package cn.springcloud.fix.demo.common;

import java.util.HashMap;
import java.util.Map;

public class FinallyTest {

    public static void main(String[] args) {
        //System.out.println(getMap().get("KEY"));//FINALLY
        //System.out.println(test11());//200

        //System.out.println(test1());//200
        //System.out.println(test2());//200
        //System.out.println(test3());//100
        //System.out.println(test4());//204
        System.out.println(test5());//35
    }

    /**
     * try block
     * return statement
     * finally block
     * after return
     */
    private static String test11() {
        try {
            System.out.println("try block");
            return test12();
        } finally {
            System.out.println("finally block");
        }
    }

    private static String test12() {
        System.out.println("return statement");
        return "after return";
    }

    private static Map<String, String> getMap() {
        Map<String, String> map = new HashMap<>();
        map.put("KEY", "INIT");
        try {
            map.put("KEY", "TRY");
            //try 执行完之后，看到return会把要返回的值保存在返回值列表中，map对象，被保存在局部变量表中了
            return map;
        } catch (Exception e) {
            map.put("KEY", "CATCH");
        } finally {
            map.put("KEY", "FINALLY");
            map = null;//map是引用类型，这里把引用类型置为null，不再关联map对象了
        }
        return map;
    }

    private static int test1() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println(22222222);
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            System.out.println(33333333);
        }
        System.out.println(111111111);
        return b;
    }

    private static int test2() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            return 200;
        }
        // return b;
    }

    private static int test3() {
        int b = 20;
        try {
            System.out.println("try block");
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            b = 150;
        }
        return 2000;
    }

    private static int test4() {
        int b = 20;
        try {
            System.out.println("try block");
            b = b / 0;
            return b += 80;
        } catch (Exception e) {
            b += 15;
            System.out.println("catch block");
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            b += 50;
        }
        return 204;
    }

    private static int test5() {
        int b = 20;
        try {
            System.out.println("try block");
            b = b / 0;
            return b += 80;
        } catch (Exception e) {
            System.out.println("catch block");
            return b += 15;
        } finally {
            System.out.println("finally block");
            if (b > 25) {
                System.out.println("b>25, b = " + b);
            }
            System.out.println(33333333);
            b += 50;
            System.out.println(44444444);
        }
        //return b;
    }
}
